import React from "react";
import MainLayout from "@/component/mainLayout/mainLayout";
import { Toast } from "antd-mobile";
import { connect } from "react-redux";
import Button from "../components/button";
import creditStore from "../creditStore";
import authorizationOfPersonalInformation from "@/static/text/authorizationOfPersonalInformation.txt";
import yhTools from "@/tools/yh";
import { InterfaceG } from "@/config/Config";
import "./index.scss";

const Authorization = props => {
	const { productNum } = props;
	const submitCredit = yhTools.getUrlParam("submitCredit"); //获取地址 submitCredit 参数，0-不调授信接口，其他-调提交授信
	const _pageParams = {
		header: {
			title: "请完善您的申请信息"
		},
		isModal: true,
		history: props.history,
		cacheLifecycles: props.cacheLifecycles
	};
	const submit = async () => {
		if (submitCredit && submitCredit == "0") {
			//不需提交授信，直接下一步
			props.goNext();
			return;
		}
		let objs = {
			url: InterfaceG.xhdSubmitCredit,
			params: {
				productNum,
				userDeviceInfo: JSON.stringify(
					props.getAppPlatformsDatas.devicesInfo || {}
				)
			}
		};
		try {
			Toast.loading("加载中...");
			const { code, message } = await yhTools.request(objs);
			if (code == "0") {
				props.goNext();
			} else {
				Toast.info(message, 2);
			}
		} catch (error) {
			Toast.fail(error ? error.message : "");
		}
	};
	return (
		<MainLayout pageParams={_pageParams} handleBack={props.handleBack}>
			<div className="xhd-authorization-wrapper">
				<span className="xhd-authorization-wrapper-p">
					同意人行征信协议即可获得小辉贷借款额度
				</span>
				<main className="xhd-authorization-wrapper-main">
					<h1 className="xhd-authorization-wrapper-main-title">
						个人征信授权书
					</h1>
					<div
						className="xhd-authorization-wrapper-main-content"
						dangerouslySetInnerHTML={{
							__html: authorizationOfPersonalInformation
						}}
					></div>
				</main>
				<Button onClick={submit} isHiddenBotton={true}>
					同意协议并开通
				</Button>
			</div>
		</MainLayout>
	);
};

export default connect(state => state)(creditStore(Authorization));
